Products
Product pages to view all products, specific products or checkout the existing cart.
Add new product to database->127.0.0.1:5000/api/addproductGet specific product by ID->127.0.0.1:5000/api/product/id/<id>Get all products->127.0.0.1:5000/api/productsAdd item to cart->127.0.0.1:5000/api/products/addEmpty cart->127.0.0.1:5000/api/products/emptyDelete item from cart->127.0.0.1:5000/api/products/delete/<string:code>View cart->127.0.0.1:5000/api/products/cartCheckout cart->127.0.0.1:5000/api/products/checkoutView all orders made->127.0.0.1:5000/api/vieworder
Add product to Database
Add function is implemented at main.py:
Method: HTTP POST
Require user to be authenticated first
| Parameters | Data Type |
|---|---|
| secretKey | String |
| name | String |
| price | Number |
| sku | String |
| image | String |
Sample input
secretKey=supersecret123
name=Apple
price=2.0
sku=AP200
image=https://commons.wikimedia.org/wiki/File:Honeycrisp-Apple.jpg
@app.route("/api/products/addproduct", methods=["POST"])
def api_addproduct():
if authenticated():
if "secretKey" in request.form:
if request.form["secretKey"] == app.secret_key:
name = request.form["name"]
price = float(request.form["price"])
sku = request.form["sku"]
image = request.form["image"]
created_at = time.time()
if (
created_at
and image
and sku
and price
and name
and request.method == "POST"
):
try:
data = {
"name": name,
"price": price,
"sku": sku,
"image": image,
"created_at": created_at,
}
rec = db.child("products").push(
data
) # push data to firebase realtime database
data_typesense = {
"id": rec["name"],
"name": name,
"price": price,
"sku": sku,
"image": image,
"created_at": created_at
}
client.collections["products"].documents.create(data_typesense)
return Response(
json.dumps({"success": True}),
status=200,
mimetype="application/json",
)
except Exception as e:
return Response(
json.dumps({"error": e}),
status=400,
mimetype="application/json",
)
else:
return Response(
json.dumps({"error": "Missing data"}),
status=400,
mimetype="application/json",
)
else:
return Response(
json.dumps({"error": "User not authenticated"}),
status=403,
mimetype="application/json",
)
else:
return Response(
json.dumps({"error": "User not authenticated"}),
status=403,
mimetype="application/json",
)
else:
return Response(
json.dumps({"error": "User not authenticated"}),
status=403,
mimetype="application/json",
)
The response will be a JSON output with a HTTP Status code, showing 200 on success and 400 or 403 otherwise
Sample JSON output
{"success": True}
{"error": "User not authenticated"}
{"error": "Missing data"}
Get specific product by ID
Retrieve function is implemented at main.py:
Method: HTTP GET
Require user to be authenticated first
| Parameters | Data Type |
|---|---|
| id | String |
id=-MqKfpAsfghPCKAu54q
/api/products/id/-MqKfpAsfghPCKAu54q
@app.route("/api/products/id/<id>", methods=["GET"])
def api_product(id):
if authenticated():
try:
products = client.collections["products"].documents[id].retrieve()
try:
return Response(
json.dumps({"success": products}),
status=200,
mimetype="application/json",
)
except Exception as e:
return Response(
json.dumps({"error": e}), status=400, mimetype="application/json"
)
except Exception as e:
return Response(
json.dumps({"error": "Product not found"}),
status=200,
mimetype="application/json",
)
else:
return Response(
json.dumps({"error": "User not authenticated"}),
status=403,
mimetype="application/json",
)
The response will be a JSON output with a HTTP Status code, showing 200 on success and 400 or 403 otherwise
Sample JSON output
Returns output with item retrieved from the database.
{
"success":{
"created_at":1638871289.549649,
"id":"-MqKfpAsfghPCKAu54q",
"image":"https://assets.adidas.com/images/h_840,f_auto,q_auto:sensitive,fl_lossy,c_fill,g_auto/e698609062b1453eacdaaa480123bd10_9366/ADVANTAGE_K_White_EF0213_01_standard.jpg",
"name":"Shoes",
"price":100.0,
"sku":"SH100"
}
}
{"error": "No products found"}
{"error": "User not authenticated"}
Get all products
Retrieve function is implemented at main.py:
Method: HTTP GET
Require user to be authenticated first
@app.route("/api/products", methods=["GET"])
def api_products():
try:
if request.method == "GET":
if authenticated():
try:
products = db.child("products").get()
output = []
for p in products.each():
output.append(
{
"id": p.key(),
"name": p.val()["name"],
"price": p.val()["price"],
"sku": p.val()["sku"],
"image": p.val()["image"],
"created_at": p.val()["created_at"]
}
)
return Response(
json.dumps({"success": output}),
status=200,
mimetype="application/json",
)
except:
return Response(
json.dumps({"error": "No products found"}),
status=200,
mimetype="application/json",
)
else:
return Response(
json.dumps({"error": "User not authenticated"}),
status=403,
mimetype="application/json",
)
except Exception as e:
return Response(
json.dumps({"error": e}), status=400, mimetype="application/json"
)
The response will be a JSON output with a HTTP Status code, showing 200 on success and 400 or 403 otherwise
Sample JSON output
Returns output with all the products available from the database.
{
"success":[
{
"created_at":1638871289.549649,
"id":"-MqKfpAsfghPCKAu54q",
"image":"https://assets.adidas.com/images/h_840,f_auto,q_auto:sensitive,fl_lossy,c_fill,g_auto/e698609062b1453eacdaaa480123bd10_9366/ADVANTAGE_K_White_EF0213_01_standard.jpg",
"name":"Shoes",
"price":100.0,
"sku":"SH100"
},
{
"created_at":1638871389.549649,
"id":"-MqKfpoAsglStrAu3qv",
"image":"https://images-na.ssl-images-amazon.com/images/I/71lA3a2cpPL.__AC_SX300_SY300_QL70_ML2_.jpg",
"name":"Headphone",
"price":400.0,
"sku":"HP400"
},
{
"created_at":1638871189.549649,
"id":"-MqKfpoajSCASKAu3qv",
"image":"https://www.americantourister.com.sg/dw/image/v2/AAWQ_PRD/on/demandware.static/-/Sites-American-Tourister/default/dw9f3d36bc/images/frontec-spinner-exp-tsa/hi-res/134992_1834_hi-res_FRONT34_1.jpg?sw=500&sh=750",
"name":"American Tourist",
"price":12000.0,
"sku":"AT1200"
},
{
"created_at":1638893839.089349,
"id":"-MqKfpoajSrRPCKAu3qv",
"image":"https://minnetonkaorchards.com/wp-content/uploads/2021/04/Bright-Red-Apple.jpg",
"name":"Apple",
"price":2.0,
"sku":"AP20"
},
{
"created_at":1638893978.2046132,
"id":"-MqKgMnAHcY4Ly6geZMC",
"image":"https://dovemed-prod-k8s.s3.amazonaws.com/media/images/bartlett-pear-1269879.height-400.width-750.png",
"name":"Pear",
"price":2.0,
"sku":"PE20"
},
{
"created_at":1638979318.7502878,
"id":"-MqPlvNcGVcOYCdK03TO",
"image":"https://i.redd.it/z60j3az3pos01.jpg",
"name":"Orange",
"price":2.0,
"sku":"OR20"
}
]
}
{"error": "No products found"}
{"error": "User not authenticated"}
Add item to cart
Add function is implemented at main.py:
Method: HTTP POST
Require user to be authenticated first
| Parameters | Data Type |
|---|---|
| name | String |
| quantity | Number |
name=-MqKfpAsfghPCKAu54q
quantity=2
@app.route("/api/products/add", methods=["POST"])
def api_add_to_cart():
if authenticated():
_quantity = int(request.form["quantity"])
_id = request.form["name"]
try:
products = client.collections["products"].documents[_id].retrieve()
try:
itemArray = {
_id: {
"id": _id,
"name": products["name"],
"sku": products["sku"],
"quantity": _quantity,
"price": products["price"],
"image": products["image"],
"total_price": _quantity * products["price"]
}
}
all_total_price = 0
all_total_quantity = 0
total_items = []
session.modified = True
if "cart_item" in session:
if _id in session["cart_item"]:
for key, value in session["cart_item"].items():
if _id == key:
old_quantity = session["cart_item"][key]["quantity"]
total_quantity = old_quantity + _quantity
session["cart_item"][key]["quantity"] = total_quantity
session["cart_item"][key]["total_price"] = (
total_quantity * products["price"]
)
else:
session["cart_item"] = array_merge(
session["cart_item"], itemArray
)
for key, value in session["cart_item"].items():
individual_quantity = int(session["cart_item"][key]["quantity"])
individual_price = float(
session["cart_item"][key]["total_price"]
)
all_total_quantity = all_total_quantity + individual_quantity
all_total_price = all_total_price + individual_price
else:
session["cart_item"] = itemArray
all_total_quantity = all_total_quantity + _quantity
all_total_price = all_total_price + _quantity * products["price"]
session["all_total_quantity"] = all_total_quantity
session["all_total_price"] = all_total_price
for item in session["cart_item"].items():
total_items.append(item)
order_data = {
"email": session["email"],
"items": total_items,
"all_total_quantity": all_total_quantity,
"all_total_price": all_total_price,
}
return Response(
json.dumps({"success": order_data}),
status=200,
mimetype="application/json",
)
except Exception as e:
return Response(
json.dumps({"error": e}), status=400, mimetype="application/json"
)
except Exception as e:
return Response(
json.dumps({"error": e}), status=400, mimetype="application/json"
)
else:
return Response(
json.dumps({"error": "User not authenticated"}),
status=403,
mimetype="application/json",
)
The response will be a JSON output with a HTTP Status code, showing 200 on success and 400 or 403 otherwise
Sample JSON output
Returns output with the items in the cart
{
"success":{
"all_total_price":200.0,
"all_total_quantity":2,
"email":"chungloong@hotmail.com",
"items":[
[
"-MqKfpAsfghPCKAu54q",
{
"id":"-MqKfpAsfghPCKAu54q",
"image":"https://assets.adidas.com/images/h_840,f_auto,q_auto:sensitive,fl_lossy,c_fill,g_auto/e698609062b1453eacdaaa480123bd10_9366/ADVANTAGE_K_White_EF0213_01_standard.jpg",
"name":"Shoes",
"price":100.0,
"quantity":2,
"sku":"SH100",
"total_price":200.0
}
]
]
}
}
{"error": "User not authenticated"}
Empty cart
Empty cart function is implemented at main.py:
Method: HTTP GET
Require user to be authenticated first
@app.route("/api/products/empty")
def api_empty_cart():
if authenticated():
try:
email = session["email"]
session.clear()
session["email"] = email
return Response(
json.dumps({"success": "Successfully emptied cart"}),
status=200,
mimetype="application/json",
)
except Exception as e:
return Response(
json.dumps({"error": e}), status=400, mimetype="application/json"
)
else:
return Response(
json.dumps({"error": "User not authenticated"}),
status=403,
mimetype="application/json",
)
The response will be a JSON output with a HTTP Status code, showing 200 on success and 400 or 403 otherwise
Sample JSON output
{"success": "Successfully emptied cart"}
{"error": "User not authenticated"}
Delete item from cart
Delete function is implemented at main.py:
Method: HTTP GET
Require user to be authenticated first
| Parameters | Data Type |
|---|---|
| id | String |
| quantity | Number |
id=-MqKfpAsfghPCKAu54q
/api/products/delete/-MqKfpAsfghPCKAu54q
@app.route("/api/products/delete/<string:code>")
def api_delete_product(code):
if authenticated():
try:
all_total_price = 0
all_total_quantity = 0
session.modified = True
total_items = []
if "cart_item" in session:
for item in session["cart_item"].items():
if item[0] == code:
session["cart_item"].pop(item[0], None)
break
else:
continue
for item in session["cart_item"].items():
total_items.append(item)
for key, value in session["cart_item"].items():
individual_quantity = int(session["cart_item"][key]["quantity"])
individual_price = float(session["cart_item"][key]["total_price"])
all_total_quantity = all_total_quantity + individual_quantity
all_total_price = all_total_price + individual_price
if all_total_quantity == 0:
email = session["email"]
session.clear()
session["email"] = email
else:
session["all_total_quantity"] = all_total_quantity
session["all_total_price"] = all_total_price
order_data = {
"email": session["email"],
"items": total_items,
"all_total_quantity": all_total_quantity,
"all_total_price": all_total_price,
}
return Response(
json.dumps({"success": order_data}),
status=200,
mimetype="application/json",
)
except Exception as e:
return Response(
json.dumps({"error": e}), status=400, mimetype="application/json"
)
else:
return Response(
json.dumps({"error": "User not authenticated"}),
status=403,
mimetype="application/json",
)
The response will be a JSON output with a HTTP Status code, showing 200 on success and 400 or 403 otherwise
Sample JSON output
Returns output with the existing item removed from cart
{
"success": {
"all_total_price": 400.0,
"all_total_quantity": 1,
"email": "chungloong@hotmail.com",
"items": [
["-MqKfpoAsglStrAu3qv", {
"id": "-MqKfpoAsglStrAu3qv",
"image": "https://images-na.ssl-images-amazon.com/images/I/71lA3a2cpPL.__AC_SX300_SY300_QL70_ML2_.jpg",
"name": "Headphone",
"price": 400.0,
"quantity": 1,
"sku": "HP400",
"total_price": 400.0
}]
]
}
}
{"error": "User not authenticated"}
View cart
View function is implemented at main.py:
Method: HTTP GET
Require user to be authenticated first
@app.route("/api/products/cart")
def api_cart():
if authenticated():
try:
all_total_price = 0
all_total_quantity = 0
session.modified = True
total_items = []
if "cart_item" in session:
for item in session["cart_item"].items():
total_items.append(item)
for key, value in session["cart_item"].items():
individual_quantity = int(session["cart_item"][key]["quantity"])
individual_price = float(session["cart_item"][key]["total_price"])
all_total_quantity = all_total_quantity + individual_quantity
all_total_price = all_total_price + individual_price
order_data = {
"email": session["email"],
"items": total_items,
"all_total_quantity": all_total_quantity,
"all_total_price": all_total_price,
}
return Response(
json.dumps({"success": order_data}),
status=200,
mimetype="application/json",
)
except Exception as e:
return Response(
json.dumps({"error": e}), status=400, mimetype="application/json"
)
else:
return Response(
json.dumps({"error": "User not authenticated"}),
status=403,
mimetype="application/json",
)
The response will be a JSON output with a HTTP Status code, showing 200 on success and 400 or 403 otherwise
Sample JSON output
Returns output with the items in the cart
{
"success": {
"all_total_price": 400.0,
"all_total_quantity": 1,
"email": "chungloong@hotmail.com",
"items": [
["-MqKfpoAsglStrAu3qv", {
"id": "-MqKfpoAsglStrAu3qv",
"image": "https://images-na.ssl-images-amazon.com/images/I/71lA3a2cpPL.__AC_SX300_SY300_QL70_ML2_.jpg",
"name": "Headphone",
"price": 400.0,
"quantity": 1,
"sku": "HP400",
"total_price": 400.0
}]
]
}
}
{"error": "User not authenticated"}
Checkout cart
Checkout cart function is implemented at main.py:
Method: HTTP POST
Require user to be authenticated first
| Parameters | Data Type |
|---|---|
| name | String |
| address | String |
| phone | Number |
name=Yunus
address=812 Yishun 15-22
phone=81234567
@app.route("/api/products/checkout", methods=["GET", "POST"])
def api_checkout():
if authenticated():
if request.method == "GET":
return Response(
json.dumps({"error": "Method not POST"}),
status=400,
mimetype="application/json",
)
else:
name = request.form["name"]
address = request.form["address"]
phone = request.form["phone"]
created_at = time.time()
try:
if (
"cart_item"
and "all_total_quantity"
and "all_total_price" in session
):
order_data = {
"name": name,
"address": address,
"phone": phone,
"email": session["email"],
"created_at": created_at,
"items": session["cart_item"],
"total_quantity": session["all_total_quantity"],
"total_price": session["all_total_price"]
}
try:
rec = db.child("orders").push(order_data)
email = session[
"email"
] # clear cart when order placed successfully
session.clear()
session["email"] = email
return Response(
json.dumps({"success": rec["name"]}),
status=200,
mimetype="application/json",
)
except Exception as e:
return Response(
json.dumps({"error": e}),
status=400,
mimetype="application/json",
)
else:
return Response(
json.dumps({"error": "No items in cart"}),
status=400,
mimetype="application/json",
)
except Exception as e:
return Response(
json.dumps({"error": e}), status=400, mimetype="application/json"
)
else:
return Response(
json.dumps({"error": "User not authenticated"}),
status=403,
mimetype="application/json",
)
The response will be a JSON output with a HTTP Status code, showing 200 on success and 400 or 403 otherwise
Sample JSON output
Returns output with the new order number placed.
{
"success": "-MqQG7lA749UQiTmRqPT"
}
{"error": "User not authenticated"}
View all orders made
View all function is implemented at main.py:
Method: HTTP GET
Require user to be authenticated first
@app.route("/api/vieworder", methods=["GET"])
def api_vieworder():
try:
if request.method == "GET":
if authenticated():
try:
products = db.child("orders").get()
output = []
for p in products.each():
if p.val()['email'] == session['email']:
output.append(
{
"id": p.key(),
"name": p.val()["name"],
"phone": p.val()["phone"],
"address": p.val()["address"],
"total_price": p.val()["total_price"],
"total_quantity": p.val()["total_quantity"],
"items": p.val()["items"],
"created_at": p.val()["created_at"]
}
)
return Response(
json.dumps({"success": output}),
status=200,
mimetype="application/json",
)
except:
return Response(
json.dumps({"error": "No orders"}),
status=400,
mimetype="application/json",
)
else:
return Response(
json.dumps({"error": "User not authenticated"}),
status=403,
mimetype="application/json",
)
except Exception as e:
return Response(
json.dumps({"error": e}), status=400, mimetype="application/json"
)
The response will be a JSON output with a HTTP Status code, showing 200 on success and 400 or 403 otherwise
Sample JSON output
Returns output with all the orders placed under the user.
{
"success":[
{
"address":"812 Yishun 15-22",
"created_at":1638987499.6712189,
"id":"-MqQG7lA749UQiTmRqPT",
"items":{
"-MqKfpoAsglStrAu3qv":{
"id":"-MqKfpoAsglStrAu3qv",
"image":"https://images-na.ssl-images-amazon.com/images/I/71lA3a2cpPL.__AC_SX300_SY300_QL70_ML2_.jpg",
"name":"Headphone",
"price":400.0,
"quantity":1,
"sku":"HP400",
"total_price":400.0
}
},
"name":"Yunus",
"phone":"81234567",
"total_price":400.0,
"total_quantity":1
}
]
}
{"error": "User not authenticated"}